home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / daemons / init / sysvinit.000 / sysvinit / sysvinit-2.64 / mesg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-15  |  1.6 KB  |  76 lines

  1. /*
  2.  * mesg.c    The "mesg" utility. Gives / restrict access to
  3.  *        your terminal by others.
  4.  *
  5.  * Usage:    mesg [y|n].
  6.  *        Without arguments prints out the current settings.
  7.  *
  8.  *        This file is part of the sysvinit suite,
  9.  *        Copyright 1991-1996 Miquel van Smoorenburg.
  10.  *
  11.  *        This program is free software; you can redistribute it and/or
  12.  *        modify it under the terms of the GNU General Public License
  13.  *        as published by the Free Software Foundation; either version
  14.  *        2 of the License, or (at your option) any later version.
  15.  */
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <errno.h>
  21. #include <unistd.h>
  22. #include <grp.h>
  23.  
  24. char *Version = "@(#) mesg 1.2 16-Apr-1996 MvS";
  25.  
  26. /*
  27.  *    Here we check if this system has a "tty" group
  28.  *    for the tty device. If it does, we set the modes
  29.  *    to -rw--w--- instead if -rw--w--w.
  30.  */
  31. int hasttygrp(struct stat *st)
  32. {
  33.   struct group *gr;
  34.  
  35.   if ((gr = getgrgid(st->st_gid)) == NULL)
  36.     return 0;
  37.   if (strcmp(gr->gr_name, "tty") != 0)
  38.     return 0;
  39.  
  40.   return 1;
  41. }
  42.  
  43. int main(int argc, char **argv)
  44. {
  45.   struct stat st;
  46.   int ttymode;
  47.  
  48.   if (!isatty(0)) {
  49.     /* Or should we look in /var/run/utmp? */
  50.     fprintf(stderr, "stdin: is not a tty");
  51.     return(1);
  52.   }
  53.  
  54.   if (fstat(0, &st) < 0) {
  55.     perror("fstat");
  56.     return(1);
  57.   }
  58.  
  59.   ttymode = hasttygrp(&st) ? 020 : 022;
  60.  
  61.   if (argc < 2) {
  62.     printf("is %s\n", ((st.st_mode & ttymode) == ttymode) ? "y" : "n");
  63.     return(0);
  64.   }
  65.   if (argc > 2 || (argv[1][0] != 'y' && argv[1][0] != 'n')) {
  66.     fprintf(stderr, "Usage: mesg [y|n]\n");
  67.     return(1);
  68.   }
  69.   if (argv[1][0] == 'y')
  70.     st.st_mode |= ttymode;
  71.   else
  72.     st.st_mode &= ~(ttymode);
  73.   fchmod(0, st.st_mode);
  74.   return(0);
  75. }
  76.